package com.wanxi.education.controller.back;

import com.github.pagehelper.PageInfo;
import com.wanxi.education.common.CommonResult;
import com.wanxi.education.model.vo.advertandcertificate.*;
import com.wanxi.education.service.ClassificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yanguangqiong
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Api(tags = "后台管理员证书分类管理")
@RestController
@RequestMapping("/admin/back/classification")
public class ClassificationBackController {
    @Autowired
    private ClassificationService classificationService;
    @PostMapping("/findPage")
    @ApiOperation("后台管理员查看证书分类列表")
    public CommonResult<PageInfo<ClassificationVo>> findPage(@RequestBody ClassificationFindPageVo classificationFindPageVo) {
        if (ObjectUtils.isEmpty(classificationFindPageVo.getPageNum())) {
            return CommonResult.failure("第几页为空");
        }
        if (ObjectUtils.isEmpty(classificationFindPageVo.getPageSize())) {
            return CommonResult.failure("每页几条为空");
        }
        PageInfo pageInfo = classificationService.findPage(classificationFindPageVo);
        return CommonResult.success(pageInfo);
    }
    @GetMapping("/findAll")
    @ApiOperation("后台管理员增加证书分类时的父类级别")
//    查找父id为0的分类
    public CommonResult<List<ClassificationForCourseVo>> findAll() {
       List<ClassificationForCourseVo>  list=classificationService.findAll();
        return CommonResult.success(list);
    }

    @GetMapping("/findById")
    @ApiOperation("后台管理员查看证书分类信息，用于修改证书分类")
    public CommonResult<ClassificationVo> findById(@RequestParam("classificationId") Long classificationId) {
        if (ObjectUtils.isEmpty(classificationId)) {
            return CommonResult.failure("证书分类Id为空");
        }
        ClassificationVo classificationVo =classificationService.findById(classificationId);
        return CommonResult.success(classificationVo);
    }

    @PostMapping("/add")
    @ApiOperation("后台管理员增加证书分类信息")
    public CommonResult add(@RequestBody ClassificationAddVo classificationAddVo){
        if (StringUtils.isBlank(classificationAddVo.getName())) {
            return CommonResult.failure("证书分类名称为空");
        }
        if (ObjectUtils.isEmpty(classificationAddVo.getPid())) {
            return CommonResult.failure("父类id为空");
        }
        if(classificationAddVo.getPid()==34){
            classificationAddVo.setPid(0L);
        }
        classificationService.add(classificationAddVo);
        return CommonResult.success("success");
    }
    @PostMapping("/update")
    @ApiOperation("后台管理员修改证书分类信息")
    public CommonResult update(@RequestBody ClassificationAddVo classificationAddVo ){
        if (StringUtils.isBlank(classificationAddVo.getName())) {
            return CommonResult.failure("证书分类名称为空");
        }
        if (ObjectUtils.isEmpty(classificationAddVo.getPid())) {
            return CommonResult.failure("父类id为空");
        }
        classificationService.update(classificationAddVo);
        return CommonResult.success("success");
    }
    @PostMapping("/delete")
    @ApiOperation("后台管理员删除证书分类信息")
    public CommonResult delete(@RequestParam("classificationId") Long classificationId){
        if (ObjectUtils.isEmpty(classificationId)) {
            return CommonResult.failure("证书分类id为空");
        }
        classificationService.delete(classificationId);
        return CommonResult.success("success");
    }
    /*
    *
    * */
    @GetMapping("/findAllForCourse")
    @ApiOperation("后台管理员管理课程绑定证书分类")
    public CommonResult<List<ClassificationForCourseVo>> findAllForCourse(){
        List<ClassificationForCourseVo> allForCourse = classificationService.findAllForCourse();
        return CommonResult.success(allForCourse);
    }
}
